import {classicBeh} from '../classic-beh.js'
const mMgr = wx.getBackgroundAudioManager()

Component({
  behaviors: [classicBeh],
  properties: {
    src: String,
    title: String
  },
  data: {
    playing: false,
    pauseIcon: 'iconfont iconstop',
    playIcon: 'iconfont iconplayfill'
  },
  lifetimes: {
    attached: function () {
      this._recoverStatus()
      this._monitorSwitch()
    }
  },
  methods: {
    onPlay() {
      if (!this.data.playing) {
        this.setData({
          playing: true
        })
        mMgr.src = this.properties.src
        mMgr.title = this.properties.title
      } else {
        this.setData({
          playing: false
        })
        mMgr.pause()
      }
    },

    _recoverStatus() {
      if (mMgr.paused) {
        this.setData({
          playing: false
        })
        return
      }
      if (mMgr.src === this.properties.src) {
        this.setData({
          playing: true
        })
      }
    },

    _monitorSwitch() {
      mMgr.onPlay(() => {
        this._recoverStatus()
      })
      mMgr.onPause(() => {
        this._recoverStatus()
      })
      mMgr.onStop(() => {
        this._recoverStatus()
      })
      mMgr.onEnded(() => {
        this._recoverStatus()
      })
    }
  },
  externalClasses: ['iconfont', 'iconplayfill', 'iconstop']
})